import {
    ActionSheetController, Content, Events, IonicPage, NavController, NavParams,
    ToastController
} from "ionic-angular";
import {ChangeDetectorRef, Component, ViewChild} from "@angular/core";
import {WebCastPlayBackListProvider} from "../../../providers/webcast/webcast-playback-list/webcast-playback-list";
import {WebCastPlayBackFilterPage} from "../webcast-playback-filter/webcast-playback-filter";
import {WebcastDetailPage} from "../webcast-detail/webcast-detail"
import {WebCastItem} from "../../../model/pages/webcast/webcast-playback-list/webcast-playback-list";
import {LoadingService} from "../../../services/common/loading.service";
import moment from "moment";
import {
    PlayBackFilterModel,
    VideoType
} from "../../../model/pages/webcast/webcast-playback-filter/webcast-playback-filter.model";


@IonicPage()
@Component({
    selector: 'page-webcast-playback-list',
    templateUrl: 'webcast-playback-list.html',
    providers: [WebCastPlayBackListProvider]
})
export class WebCastPlayBackListPage {
    private playbackList: WebCastItem[] = [];
    private isOrderUp: boolean = false;
    private isScrolling: boolean = false;
    private isArriveBottom: boolean = false;
    //列表数据是否加载完毕
    private isLoaded = false;
    private playbackFil: PlayBackFilterModel = new PlayBackFilterModel();
    private sorters: any[] = [{
        name: '按时间从新到旧',
        value: 0,
        selected: true
    }, {
        name: '按时间从旧到新',
        value: 1,
        selected: false
    }];
    //视频类型列表
    private typeList: VideoType[] = [];
    private title:string ="回放";
    private recommendBgImgUrl: string = './assets/images/webcast/live_default.png';
    @ViewChild(Content) content: Content;

    constructor(private navCtrl: NavController,
                public navParams: NavParams,
                private actionSheetCtrl: ActionSheetController,
                private events: Events,
                private changeRef: ChangeDetectorRef,
                private webcasePlaybackPro: WebCastPlayBackListProvider,
                private load: LoadingService,
                private toast: ToastController,
    ) {
    }
    ionViewDidLoad() {
        console.log("enter view")
        this.playbackFil.type = this.navParams.data.type || 0;
        this.getAllPlayType();
    }
    getAllPlayType() {
        this.isLoaded = false;
        this.webcasePlaybackPro.getVideoTypes().subscribe((res: Array<VideoType>) => {
            this.typeList = res;
            this.typeList.unshift({name: "全部", id: 0});
            this.getPlaybackList(this.playbackFil);
        },err=>{
            this.isLoaded = true;
        });
    }

    getPlaybackList(params) {
        let fiterParam = {...params};
        this.isLoaded = false;
        fiterParam.starttime = moment(new Date(params.starttime)).unix();
        fiterParam.endtime = moment(new Date(params.endtime)).unix();
        this.load.showLoading();
        this.webcasePlaybackPro.getPlayBackList(fiterParam).subscribe((res: WebCastItem[]) => {
             let items = res;
            items.forEach(item=>{
                item.webcastImgUrl = item.webcastImgUrl?item.webcastImgUrl:this.recommendBgImgUrl;
            });
            this.playbackList = items;
            setTimeout(() => {
                this.isLoaded = true;
            }, 300);
            this.load.dismissLoading();
        },err=>{
            this.isLoaded = true;
            this.load.dismissLoading();
        });
    }

    sorterPlaybackList() {
        this.isOrderUp = !this.isOrderUp;
        this.playbackFil.order = this.isOrderUp == true ? 1 : 0;
        this.getPlaybackList(this.playbackFil);
    }

    goToPlayBackFilter() {
        this.events.unsubscribe('filter-events');
        this.events.subscribe('filter-events', (params) => {
            this.playbackFil = params;
            this.isLoaded = false;
            this.title = "筛选结果";
            this.events.unsubscribe('filter-events');
            this.getPlaybackList(this.playbackFil);
        });
        this.navCtrl.push("WebCastPlayBackFilterPage", this.playbackFil);
    }

    goToPlayerPage(item) {
        this.navCtrl.push("WebcastDetailPage", {
            castId: item.id,
            castTitle: item.subject,
            roomId: item.webinarId
        });
    }

    unixResolve(time) {
        if (time) {
            return moment.unix(time).format('YYYY/MM/DD')
        } else {
            return '';
        }
    }

    typeResolve(type) {
        let typeStr = '';
        this.typeList.forEach((item) => {
            if (item.id == type) {
                typeStr = item.name;
            }
        })
        return typeStr;
    }

    doRefresh(refresher) {
        this.getPlaybackList(this.playbackFil);
        setTimeout(() => {
            refresher.complete();
        }, 1000);
    }

    goUp() {
        if (this.isScrolling) {
            setTimeout(() => {
                this.goUp();
            }, 300);
            return;
        }
        this.content.scrollToTop();
    }

    scrollStart(e) {
        this.isScrolling = true;
    }

    scrollEnd(e) {
        this.isScrolling = false;
    }

    scrolling() {
        const dimensions = this.content.getContentDimensions();
        const temp_height = dimensions.scrollHeight - dimensions.scrollTop - dimensions.contentHeight < 100;

        if (temp_height !== this.isArriveBottom) {
            this.isArriveBottom = temp_height;
            this.changeRef.detectChanges();
        }
    }
}
